﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Text;
using System.Globalization;

namespace OpenPhotoAlbum.Shared
{
	/// <summary>
	/// Summary description for Albums
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	[System.Web.Script.Services.ScriptService]
	public class Albums : AlbumWebService
	{
		private const int PAGE_SIZE = int.MaxValue;
		private const int PAGE_SIZE_IN_LIST = 4;

		[WebMethod]
		public string GetAlbums(Guid userId, int pageNumber, bool isAlbumList)
		{
			var pageSize = isAlbumList ? PAGE_SIZE_IN_LIST : PAGE_SIZE;

			IEnumerable<DbAccess.Album> albums = BusinessLogics.AlbumManager.Instance.GetAlbums(userId).Skip((pageNumber - 1) * pageSize).Take(pageSize);
			if (albums == null || albums.Count() == 0) { return null; }

			List<AlbumDataItem> results = new List<AlbumDataItem>(albums.Count());

			foreach (DbAccess.Album album in albums)
			{
				Guid coverPhotoId = BusinessLogics.AlbumManager.Instance.GetAlbumCover(album.AlbumId);

				results.Add(new AlbumDataItem()
				{
					Id = album.AlbumId,
					Title = HttpUtility.HtmlEncode(album.AlbumTitle),
					Description = HttpUtility.HtmlEncode(album.AlbumDescription).Replace("\n", "<br />"),
					CreationDate = string.Format(
						CultureInfo.CurrentCulture,
						Resources.Strings.My_Album_CreationDate,
						album.AlbumCreationDate.ToShortDateString()),
					ModificationDate = (album.AlbumModificationDate.HasValue) ? 
						string.Format(
							CultureInfo.CurrentCulture, 
							Resources.Strings.My_Album_LastModificationDate,
							album.AlbumModificationDate.Value.ToShortDateString()) : null,
					CoverPhotoId = coverPhotoId,
					PhotoPipeUrl = UrlContext.BuildPhotoPipeUrl(coverPhotoId, BusinessLogics.PhotoTypes.AlbumCover),
					SharedAlbumUrl = UrlContext.BuildSharedAlbumUrl(album.AlbumId),
					PhotoCount = string.Format(
						CultureInfo.CurrentCulture,
						Resources.Strings.Shared_PhotoCount,
						album.Photos.Count)
				});
			}

			return (new JavaScriptSerializer()).Serialize(results);
		}

		[WebMethod]
		public int GetTotalPages(Guid userId, bool isAlbumList)
		{
			var pageSize = isAlbumList ? PAGE_SIZE_IN_LIST : PAGE_SIZE;

			DbAccess.User user = BusinessLogics.UserManager.Instance.GetUser(userId);
			int albumCount = user.Albums.Count;
			if (albumCount % pageSize == 0) { return albumCount / pageSize; }
			else { return albumCount / pageSize + 1; }
		}

		[WebMethod]
		public int GetPageNumberOfAlbumInList(Guid userId, Guid albumId)
		{
			int index = BusinessLogics.AlbumManager.Instance.GetAlbumIndex(userId, albumId);
			if (index == -1) { throw new Exception(); }

			return index / PAGE_SIZE_IN_LIST + 1;
		}
	}

	public class AlbumDataItem
	{
		public Guid Id { get; set; }
		public string Title { get; set; }
		public string Description { get; set; }
		public string CreationDate { get; set; }
		public string ModificationDate { get; set; }
		public Guid CoverPhotoId { get; set; }
		public string PhotoPipeUrl { get; set; }
		public string SharedAlbumUrl { get; set; }
		public string PhotoCount { get; set; }
	}
}
